package org.csu.wuliu.controller;


import com.alibaba.fastjson.JSONObject;
import org.csu.wuliu.domain.Order;
import org.csu.wuliu.domain.User;
import org.csu.wuliu.service.OrderService;
import org.csu.wuliu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@ResponseBody
public class UserController {

    @Autowired
    private UserService userService;


    /**
     * {
     *     "username": "hkx",
     *     "password": "123",
     *     "address": "长沙",
     *     "tel": "123456789"
     * }
     */
    @PostMapping("/user/register")
    public User register(@RequestBody User user){
        System.out.println(user.getUsername());
        userService.insertUser(user);
        return user;
    }


    @GetMapping("/user/{username}/viewInfo")
    public User getUserByUsername(@PathVariable String username){
        System.out.println(username);
        User user = userService.getUserByusername(username);
        return user;
    }

    @CrossOrigin
    @RequestMapping(value = "/user/signon",method = RequestMethod.POST)
//    @PostMapping("/user/signon")
    public JSONObject signon(@RequestBody String jsonString){
        JSONObject json = JSONObject.parseObject(jsonString);
        JSONObject jsonResult = new JSONObject();
        User user = new User();
        user.setUsername(json.getString("username"));
        user.setPassword(json.getString("password"));
        System.out.println(user.getUsername()+user.getPassword());
        User temp = new User();
        temp = userService.getUserByusernameAndPassword(user);
        jsonResult.put("user",temp);
        return  jsonResult;
    }

    //{"username":"hkx","password":"12345","address":"cs","tel":"123456789"}
    @PostMapping("/user/editInfo")
    public ResponseEntity<String> editInfo(@RequestBody User user){
        userService.updateUser(user);
        return new ResponseEntity<String>("成功", HttpStatus.OK);
    }


    //{"username":"hkx"}
    @GetMapping("/user/delete")
    public ResponseEntity<String> deleteInfo(@RequestBody User user){
        userService.deleteUser(user);
        return new ResponseEntity<String>("成功", HttpStatus.OK);
    }




}
